package com.iflysse.servlet.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iflysse.pojo.RoleInfo;
import com.iflysse.pojo.TreeJson;
import com.iflysse.pojo.UserInfo;
import com.iflysse.pojo.UserRoleInfome;
import com.iflysse.service.RoleInfoService;
import com.iflysse.service.UserRoleInfoServiceme;

/**
 * Servlet implementation class UserRoleServlet
 */
@WebServlet("/user/UserRoleServlet")
public class UserRoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserRoleServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String mtd = request.getParameter("method");
		if ("allList".equals(mtd)) {
			query(request, response);
		} else if ("selectList".equals(mtd)) {
			querySelect(request, response);
		} else if ("jsontree".equals(mtd)) {
			myjosn(request, response);
		} else if ("tijiao".equals(mtd)) {
			saveRo(request, response);
		}

	}

	/**
	 * 保存选中的角色,并且删除没有选中的角色
	 */
	private void saveRo(HttpServletRequest request, HttpServletResponse response) {
		String userid = request.getParameter("userid");
		String roleids = request.getParameter("roleids");

//		System.out.println(userid);
//		System.out.println(roleids);

		String[] mds = roleids.split(",");
		UserRoleInfoServiceme uservice = new UserRoleInfoServiceme();

		// 删除原先数据库中该userid对应的所有的角色
		UserInfo user = new UserInfo();
		user.setUserid(Integer.parseInt(userid));
		uservice.TestDeleteAllUserRole(user);// 根据userid删除角色

		// 然后再进行插入操作 插入用户所选择的角色
		for (int i = 0; i < mds.length; i++) {
			UserRoleInfome ur = new UserRoleInfome();
			ur.setRoleid(Integer.parseInt(mds[i]));
			ur.setUserid(Integer.parseInt(userid));
			uservice.insertUserRole(ur);
		}

	}

	private void myjosn(HttpServletRequest req, HttpServletResponse resp) {

		RoleInfoService roleservice = new RoleInfoService();
		List<TreeJson> parents = LoadBigTree(roleservice);

		try {
			print2Page(resp, parents.toString());

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void print2Page(HttpServletResponse resp, String json)
			throws IOException {
		resp.setContentType("application/json;charset=UTF-8");
		Writer writer = resp.getWriter();
		writer.write(json);
		writer.flush();
	}

	/**
	 * 返回权限的最大树
	 */
	private List<TreeJson> LoadBigTree(RoleInfoService roleservice) {
		List<RoleInfo> rolelist = roleservice.findAllRole();

		List<TreeJson> parents = new ArrayList<TreeJson>();// 存放所有的角色的父treelist

		for (RoleInfo rinfo : rolelist) {
			TreeJson tj = new TreeJson();
			int id = rinfo.getRoleId();
			String name = rinfo.getRoleName();
			tj.setId(id);
			tj.setText(name);

			parents.add(tj);

		}

		return parents;
	}

	/**
	 *
	 * @author: Administrator
	 * @createTime: 2014年11月26日 上午9:33:38
	 * @history:
	 * @param request
	 * @param response
	 *            void
	 */
	private void querySelect(HttpServletRequest request,
			HttpServletResponse response) {

		String id = request.getParameter("userid");
//		System.out.println(id);
		int userId = Integer.parseInt(id);

		UserRoleInfoServiceme service = new UserRoleInfoServiceme();
		List<RoleInfo> list = service.findRoleByUser(userId);
//		System.out.println(list);
	}

	/**
	 *
	 * @author: Administrator
	 * @createTime: 2014年11月26日 上午9:19:48
	 * @history:
	 * @param request
	 * @param response
	 *            void
	 */
	private void query(HttpServletRequest request, HttpServletResponse response) {

		RoleInfoService service = new RoleInfoService();
		List<RoleInfo> list = service.findAllRole();
//		System.out.println(list);
		PrintWriter out;
		try {
			response.setContentType("application/json;charset=utf-8");
			out = response.getWriter();
			out.println(list.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
